package io.renren.modules.course.controller;

import io.renren.common.utils.R;
import io.renren.modules.course.entity.CourseClassificationEntity;
import io.renren.modules.course.entity.CourseSmallClassification;
import io.renren.modules.course.service.CourseClassificationService;
import io.renren.modules.course.service.CourseSmallClassificationService;
import io.renren.modules.sys.entity.SysMenuEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("course/courseSmallClassification")
public class CourseSmallClassificationController {

@Autowired
    CourseSmallClassificationService courseSmallClassificationService;

    /**
     * 递归获取微课分类
     * @param id
     * @return
     */
    @GetMapping("getCourseSmallClassification")
    public R getCourseSmallClassification(@RequestParam int id){
        List<CourseSmallClassification> list =  courseSmallClassificationService.getCourseSmallClassification(id);
        return R.ok().put("data",list);
    }


    /**
     * 添加分类
     */
    @PostMapping("insertCourseSmallClassification")
    public R insertCourseSmallClassification(@RequestBody CourseSmallClassification courseSmallClassification){
        List<CourseSmallClassification> courseSmallClassificationList  = courseSmallClassificationService.getAllCourseSmallClassification();

        for(CourseSmallClassification courseSmallClassification1 : courseSmallClassificationList){
            if(courseSmallClassification1.getName().equals(courseSmallClassification.getName().trim())){
                return R.error(102,"已有此分类信息,不可重复");
            }
        }

        courseSmallClassificationService.insert(courseSmallClassification);

        return R.ok();
    }


    /**
     * 删除分类
     */
    @GetMapping("delCourseSmallClassification")
    public R delCourseSmallClassification(@RequestParam int id){

        courseSmallClassificationService.deleteById(id);

        return R.ok();
    }


    /**
     * 获取分类信息
     */
    @GetMapping("selectCourseSmallById")
    public R selectCourseSmallById(@RequestParam int id){

       CourseSmallClassification courseSmallClassification =  courseSmallClassificationService.selectById(id);

        return R.ok().put("data",courseSmallClassification);
    }


    /**
     * 修改分类信息
     */
    @PostMapping("updateCourseSmallClassification")
    public R updateCourseSmallClassification(@RequestBody CourseSmallClassification courseSmallClassification){

        courseSmallClassificationService.updateById(courseSmallClassification);

        return R.ok();
    }
}
